package cn.innosmart.aq.customize.addview.AddProperty;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.customize.addview.ChangeTypeDialog.ChangeTypeDialog;

/**
 * Created by David on 2015/12/2.
 */
public class AddPropertyAdapter extends BaseAdapter {
    private ArrayList<Property> properties;
    private Context context;
    private LayoutInflater mInflater;
    private View.OnClickListener onDeleteClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Property property = (Property) v.getTag();
            if (properties != null)
                reMoveView(property);
        }
    };

    private View.OnClickListener onAddClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addView(new Property());
        }
    };
    private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            Property property = (Property) v.getTag();
            if (hasFocus) {
                // 此处为得到焦点时的处理内容
            } else {
                // 此处为失去焦点时的处理内容
                EditText editText = (EditText) v;
                String title = editText.getText().toString().trim();
                property.setTitle(title);
            }
        }
    };
    private View.OnClickListener onChangeTypeClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Property property = (Property) v.getTag();
            showChangeTypeDialog(property);
        }
    };


    public AddPropertyAdapter(Context context, ArrayList<Property> property) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.properties = property;
        if (properties == null) {
            properties = new ArrayList<>();
            properties.add(new Property());
        } else {
            if (properties.size() == 0)
                properties.add(new Property());
        }
    }

    private void showChangeTypeDialog(final Property property) {
        final ChangeTypeDialog changeTypeDialog = new ChangeTypeDialog(context, property);
        changeTypeDialog.setPositiveButton(context.getString(R.string.sure), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeTypeDialog.checkChange();
                changeTypeDialog.dismiss();
                notifyDataSetChanged();
            }
        });
        changeTypeDialog.setNegativeButton(context.getString(R.string.cancel), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeTypeDialog.dismiss();
            }
        });
        changeTypeDialog.setOnChangeListener(new ChangeTypeDialog.OnChangeListener() {
            @Override
            public void onChange(String type, boolean isRequired) {
                property.setType(type);
                property.setIsRequired(isRequired);
            }
        });
        changeTypeDialog.show();
    }

    @Override
    public int getCount() {
        return properties.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return properties.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        ViewHolder holder;
//        if (convertView == null) {
//            holder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.listview_add_property, null);
        ImageView ivDeleteProperty = (ImageView) convertView.findViewById(R.id.iv_delete_property);
        EditText etPropertyTitle = (EditText) convertView.findViewById(R.id.et_property_title);
        LinearLayout llChangeType = (LinearLayout) convertView.findViewById(R.id.ll_change_type);
        TextView tvType = (TextView) convertView.findViewById(R.id.tv_type);
        RelativeLayout rlProperty = (RelativeLayout) convertView.findViewById(R.id.rl_property);
        RelativeLayout rlAddlayout = (RelativeLayout) convertView.findViewById(R.id.rl_addlayout);
//            convertView.setTag(holder);
//        } else {
//            holder = (ViewHolder) convertView.getTag();
//        }
        if (position == properties.size()) {
            rlAddlayout.setVisibility(View.VISIBLE);
            rlProperty.setVisibility(View.INVISIBLE);
            rlAddlayout.setOnClickListener(onAddClickListener);
        } else {
            rlAddlayout.setVisibility(View.INVISIBLE);
            rlProperty.setVisibility(View.VISIBLE);
            final Property property = properties.get(position);
            ivDeleteProperty.setTag(property);
            ivDeleteProperty.setOnClickListener(onDeleteClickListener);
            etPropertyTitle.setText(property.getTitle());
//            holder.tvType.setText(property.getType());
            String type = context.getString(R.string.property_text);
            if (property.getType().equals("TEXT")) {
            } else if (property.getType().equals("NUM")) {
                type = context.getString(R.string.property_num);
            } else if (property.getType().equals("DATE")) {
                type = context.getString(R.string.property_date);
            }
            if (property.isRequired()) {
                tvType.setText(type + context.getString(R.string.isrequired));
            } else {
                tvType.setText(type);
            }
            etPropertyTitle.setTag(property);
            etPropertyTitle.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                             property.setTitle(s.toString());
                }
            });
//            etPropertyTitle.setOnFocusChangeListener(onFocusChangeListener);
//            property.setTag(etPropertyTitle);
            llChangeType.setTag(property);
            llChangeType.setOnClickListener(onChangeTypeClickListener);
        }
        return convertView;
    }


    public void addView(Property property) {
        if (properties == null)
            properties = new ArrayList<>();
        properties.add(property);
        notifyDataSetChanged();
    }


    public void reMoveView(Property property) {
        if (properties == null)
            return;
        properties.remove(property);
        notifyDataSetChanged();
    }

    public boolean checkProperty() {
        if (properties == null)
            properties = new ArrayList<>();
        for (Property property : properties) {
            if (TextUtils.isEmpty(property.getTitle())) {
                return true;
            }
        }
        return false;
    }


    public ArrayList<Property> getProperties() {
        return properties;
    }

    public void setProperties(ArrayList<Property> properties) {
        this.properties = properties;
    }

    static class ViewHolder {
        ImageView ivDeleteProperty;
        TextView etPropertyTitle;
        LinearLayout llChangeType;
        TextView tvType;
        RelativeLayout rlProperty;
        RelativeLayout rlAddlayout;
    }
}